import {
  request,
  chooseLocation
} from '../../utils/requestUtils';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeShow: false, // 摊位类型控制
    form: {
      stallName: '',
      typeName: '',
      longitude: '', // 经度,
      latitude: '',
      location: '',
      contact: '',
      telephone: '',
      pic: '',
      deviceId:''
    }
  },
  // 输入框方法
  onChange(event) {
    console.log('event',event)
    switch (event.currentTarget.dataset['input']) {
      case 'stallName':
        this.setData({
          "form.stallName": event.detail,
        })
        break;
      case 'contact':
        this.setData({
          "form.contact": event.detail,
        })
        break;
      case 'telephone':
        this.setData({
          "form.telephone": event.detail,
        })
        break;
      case 'location':
        this.setData({
          "form.location": event.detail,
        })
        break;
        case 'deviceId':
        this.setData({
          "form.deviceId": event.detail,
        })
        break;
    }
  },
  //类别弹窗
  chooseType() {
    this.setData({
      typeShow: true
    })
  },
  typeClose() {
    this.setData({
      typeShow: false
    })
  },
  //取消选择
  typeCancel() {
    this.setData({
      typeShow: false
    })
  },
  //确认
  typeConfirm(value) {
    this.setData({
      "form.typeName": value.detail.dictLabel,
      "form.type": value.detail.dictValue,
      typeShow: false
    })
  },
  //文件上传完成
  uploadHandler(value) {
    if (value.detail && value.detail.length > 0) {
      console.log('value.detail', value.detail)
      this.setData({
        "form.pic": value.detail[0].url,
      })
    }
  },
  //获取当前位置
  async chooseLocationHander() {
    const result = await chooseLocation()
    console.log('result', result)
    this.setData({
      "form.longitude": result.longitude,
      "form.latitude": result.latitude,
      "form.location": result.address
    })
  },
  //添加摊位
  async submit_btn() {
    let nameCheck = this.checkFn('stallName', 'stallNameErr', '摊位名称不能为空')
    let positionCheck = true; // this.checkFn('location', 'locationErr', '位置不能为空')
    let typeNameCheck = this.checkFn('typeName', 'typeNameErr', '类型不能为空')
    let contactCheck = this.checkFn('contact', 'contactErr', '联系人不能为空')
    let telephoneCheck = this.checkFn('telephone', 'telephoneErr', '联系人电话不能为空')
    let logoCheck = this.checkFn('pic', 'picErr', '摊位图片不能为空')
    if(!logoCheck){
      wx.showToast({
        title: '摊位图片不能为空',
        icon: 'none'
      })
      return
    }
    if (nameCheck && positionCheck && typeNameCheck && contactCheck && telephoneCheck && logoCheck) {
      const result = await request({
        url: '/merchant/bus/stall',
        method: "POST",
        data: {
          ...this.data.form
        }
      });
      console.log('result', result)
      if (result.data.code == 200) {
        wx.navigateBack({
          delta: 1
        });
      }
    } else {
      console.log("222")
    }
  },

  // checkFn方法表单验证
  checkFn(err, errKey, errStr) {
    console.log('this.data.form', this.data.form)

    console.log('this.data.form[err]', this.data.form[err])
    if (!this.data.form[err].trim()) {
      this.setData({
        [errKey]: errStr
      })
      return false
    } else {
      this.setData({
        [errKey]: ''
      })
      return true
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getStallInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // 获取摊位信息
  async getStallInfo() {
    const result = await request({
      url: '/merchant/bus/stall/my',
      method: "POST",
      data: {}
    });
    if (result.data.code == 200 && result.data.data.id) {
      console.log('data', result.data.data)
      this.setData({
        form: result.data.data
      })
      let component = this.selectComponent('#uploader')
      console.log('component', component)
      if (component && result.data.data.pic) {
        component.initListHandler([{
          url: result.data.data.pic
        }])
      }
    }
  }
})
